package com.tz.marlon.web.article;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.marlon.bean.Article;
import com.tz.marlon.bean.TzParams;
import com.tz.marlon.dao.article.IArticleDao;

@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private IArticleDao articleDao;
	
	@RequestMapping("/index")
	public ModelAndView index(TzParams params){
		ModelAndView modelAndView = new ModelAndView();
		params.setIsDelete(1);
		List<HashMap<String, Object>> articles = articleDao.listMaps(params);
		modelAndView.addObject("articles", articles);
		modelAndView.setViewName("article/index");
		return modelAndView;
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable("id")Integer id){
		HashMap<String, Object> article = articleDao.mapById(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("article", article);
		modelAndView.setViewName("article/detail");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String saveArticle(Article article){
		article.setIsDelete(1);
		article.setStatus(1);
		articleDao.save(article);
		return "success";
	}
}
